package com.dunqi.gpm.shunqing.home.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.activity.UpListActivity;
import com.dunqi.gpm.shunqing.home.bean.Project;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.NormalView;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Created by 空 on 2017/11/22 0022.
 */

public class ProjectBasicInfoFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private static final String PROJECTID = "projectId";
    @BindView(R.id.content_tv)
    TextView contentTv;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.total_tv)
    TextView totalTv;
    @BindView(R.id.project_states_tv)
    TextView projectStatesTv;
    @BindView(R.id.project_how_important_tv)
    TextView projectHowImportantTv;
    @BindView(R.id.industrial_development_tv)
    TextView industrialDevelopmentTv;
    LinearLayout planClickLl;
    @BindView(R.id.leader_tv)
    NormalView leaderTv;
    @BindView(R.id.company_tv)
    NormalView companyTv;
    @BindView(R.id.time_limit_tv)
    NormalView timeLimitTv;
    @BindView(R.id.contact_name_tv)
    NormalView contactNameTv;
    @BindView(R.id.contact_phone_tv)
    NormalView contactPhoneTv;

    @BindView(R.id.build_year_tv)
    NormalView buildYearTv;
    @BindView(R.id.company_leader_tv)
    NormalView companyLeaderTv;
    @BindView(R.id.company_leader_phone_tv)
    NormalView companyLeaderPhoneTv;
    @BindView(R.id.up_list_tv)
    TextView up_list_tv;

    private Project project;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryBaseInfo(project.getProjectId())
                .compose(RxSchedulers.<Data<Project>>io_main())
                .subscribe(new ComObserver<Project>() {
                    @Override
                    public void onSuccess(Project project) {
                        if (project == null) {
                            return;
                        }
                        refreshUi(project);
                    }

                    @Override
                    public void onFinal() {
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });
    }


    private void refreshUi(Project project) {
        contentTv.setText(project.getConstructionSizeContent() + "");
        totalTv.setText(project.getTotalInvestmentAmount() + "");
        timeLimitTv.setText(project.getCompleteLimitTime() + "");

        projectStatesTv.setText(project.getProjectSourceText() + "");
        projectHowImportantTv.setText(project.getImportantTypeText() + "");
        industrialDevelopmentTv.setText(project.getFirstIndustryText() + "");

        contactNameTv.setText(project.getBizPersonName() + "");
        contactPhoneTv.setText(project.getBizPersonPhone() + "");

        leaderTv.setText(project.getLeaderUserName() == null ? "" : project.getLeaderUserName());
        companyTv.setText(project.getRespOrgName() + "");

        String buildYearString;
        if (project.getPlanEndYear() == 0) {
            buildYearString = project.getPlanStartYear() + "年起";
        } else {
            buildYearString = project.getPlanStartYear() + "年至" + project.getPlanEndYear() + "年";
        }
        buildYearTv.setText(buildYearString);

        companyLeaderTv.setText(project.getRespOrgLeaderName());

        companyLeaderPhoneTv.setText(project.getRespOrgLeaderPhone());
    }


    @Override
    protected void initUiAndListener() {
        SwipeUtil.init(swipeRefresh);

        swipeRefresh.setOnRefreshListener(this);

        up_list_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        up_list_tv.getPaint().setAntiAlias(true);//抗锯齿

        up_list_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpListActivity.start(context,project.getProjectId());
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_basic_info;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        project = (Project) arguments.getSerializable(Constance.PROJECT);
    }

    public static ProjectBasicInfoFragment newInstance(Project project) {
        ProjectBasicInfoFragment projectBasicInfoFragment = new ProjectBasicInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constance.PROJECT, project);
        projectBasicInfoFragment.setArguments(bundle);
        return projectBasicInfoFragment;

    }

    @Override
    public void onRefresh() {
        net();
    }

}
